/**
 * API请求的模块化处理
 * 将项目中所有的请求进行统一管理
 */
const URLS = {
  hotUrl: 'https://m.douban.com/rexxar/api/v2/subject_collection/movie_showing/items',
  latestUrl: 'https://m.douban.com/rexxar/api/v2/subject_collection/movie_latest/items',
  freeUrl: 'https://m.douban.com/rexxar/api/v2/subject_collection/movie_free_stream/items',
  detailUrl: 'https://m.douban.com/rexxar/api/v2/movie/',
  searchUrl: 'https://api.doubans.com/v2/search/movie/douban/details',
  tvUrl: 'https://m.douban.com/rexxar/api/v2/subject_collection/tv_hot/items',
  varietyShowUrl: 'https://m.douban.com/rexxar/api/v2/subject_collection/tv_variety_show/items',
  tvDetailUrl: 'https://m.douban.com/rexxar/api/v2/tv/',
  varietyShowDetailUrl: 'https://m.douban.com/rexxar/api/v2/tv/',

}
const loadHotFilms = (params = {}) => {
  return new Promise((resolve, reject) => {
    wx.request({
      url: URLS.hotUrl,
      data: params,
      success: resolve,
      fail: reject
    })
  }).then(res => {
    if (res.statusCode === 200) {
      res.data.method = 'loadHotFilms';
      return res.data;
    } else {
      return Promise.reject({
        message: res.errMsg
      })
    }
  }).catch(err => {
    wx.showToast({
      title: '数据加载失败：' + err,
      image: '/imgs/error.png',
      duration: 3000
    })
  })
}
const loadLatesFilms = (params = {}) => {
  return new Promise((resolve, reject) => {
    wx.request({
      url: URLS.latestUrl,
      data: params,
      success: resolve,
      fail: reject
    })
  }).then(res => {
    if (res.statusCode === 200) {
      res.data.method = 'loadLatesFilms';
      return res.data;
    } else {
      // 将状态从resolve转换为reject
      return Promise.reject({
        message: res.errMsg
      })
    }
  }).catch(err => {
    wx.showToast({
      title: '数据加载失败：' + err,
      image: '/imgs/error.png',
      duration: 3000
    })
  })
}
const loadFreeFilms = (params = {}) => {
  // 返回一个Promise对象
  return new Promise((resolve, reject) => {
    wx.request({
      url: URLS.freeUrl,
      data: params,
      success: resolve,
      fail: reject
    })
  }).then(res => {
    if (res.statusCode === 200) {
      res.data.method = 'loadFreeFilms';
      return res.data;
    } else {
      return Promise.reject({
        message: res.errMsg
      })
    }
  }).catch(err => {
    wx.showToast({
      title: '数据加载失败：' + err,
      image: '/imgs/error.png',
      duration: 3000
    })
  })
}
const loadFilmByFilmId = (params = {}) => {
  return new Promise((resolve, reject) => {
    let type = params.type;
    let id = params.id;
    let url = '';
    if (type === 'movie') {
      url = URLS.detailUrl + id;
    } else if (type === 'tv') {
      url = URLS.tvDetailUrl + id;
    } else {
      url = URLS.varietyShowDetailUrl + id;
    }
    wx.request({
      url: url,
      success: resolve,
      fail: reject
    })
  }).then(res => {
    if (res.statusCode === 200) {
      return res.data
    } else {
      return Promise.reject({
        message: res.errMsg
      })
    }
  }).catch(err => {
    wx.showToast({
      title: '数据加载失败：' + err,
      image: '/imgs/error.png',
      duration: 3000
    })
  })
}
const loadCommentsByFilmId = (params = {}) => {
  return new Promise((resolve, reject) => {
    let type = params.type;
    let id = params.id;
    let url = '';
    if (type === 'movie') {
      url = URLS.detailUrl + id;
    } else if (type === 'tv') {
      url = URLS.tvDetailUrl + id;
    } else {
      url = URLS.varietyShowDetailUrl + id;
    }
    wx.request({
      url: url + '/interests',
      success: resolve,
      fail: reject
    })
  }).then(res => {
    if (res.statusCode === 200) {
      return res.data
    } else {
      return Promise.reject({
        message: res.errMsg
      })
    }
  }).catch(err => {
    wx.showToast({
      title: '数据加载失败：' + err,
      image: '/imgs/error.png',
      duration: 3000
    })
  })
}
const loadSearchFilms = (params) => {
  return new Promise((resolve, reject) => {
    wx.request({
      url: URLS.searchUrl,
      data: params,
      success: resolve,
      fail: reject
    })
  }).then(res => {
    if (res.statusCode === 200) {
      return res.data
    } else {
      return Promise.reject({
        message: res.errMsg
      })
    }
  }).catch(err => {
    wx.showToast({
      title: '数据加载失败：' + err,
      image: '/imgs/error.png',
      duration: 3000
    })
  })
}
const loadTvFilms = (params) => {
  return new Promise((resolve, reject) => {
    wx.request({
      url: URLS.tvUrl,
      data: params,
      success: resolve,
      fail: reject
    })
  }).then(res => {
    if (res.statusCode === 200) {
      res.data.method = 'loadTvFilms';
      return res.data
    } else {
      return Promise.reject({
        message: res.errMsg
      })
    }
  }).catch(err => {
    wx.showToast({
      title: '数据加载失败：' + err,
      image: '/imgs/error.png',
      duration: 3000
    })
  })
}
const loadVarietyShowFilms = (params) => {
  return new Promise((resolve, reject) => {
    wx.request({
      url: URLS.varietyShowUrl,
      data: params,
      success: resolve,
      fail: reject
    })
  }).then(res => {
    if (res.statusCode === 200) {
      res.data.method = 'loadVarietyShowFilms';
      return res.data
    } else {
      return Promise.reject({
        message: res.errMsg
      })
    }
  }).catch(err => {
    wx.showToast({
      title: '数据加载失败：' + err,
      image: '/imgs/error.png',
      duration: 3000
    })
  })
}
// 导出模块，对外暴露接口
module.exports = {
  loadHotFilms,
  loadLatesFilms,
  loadFreeFilms,
  loadFilmByFilmId,
  loadCommentsByFilmId,
  loadSearchFilms,
  loadTvFilms,
  loadVarietyShowFilms,
}